/*
 * @Descripttion: 
 * @version: 
 * @Author: guangyunl
 * @Date: 2024-01-20 18:14:24
 * @LastEditors: guangyunl
 * @LastEditTime: 2024-03-28 19:30:57
 */
/* indent size: 2 */

module.exports = app => {
    const DataTypes = app.Sequelize;
  
    const Shoppingcarts_products = app.model.define('shoppingcarts_products', {
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        comment: '购物车商品关联表id'
      },
      pid: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品id',
        references: {
            model: 'products',
            key: 'id'
          }
      },
      sid: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '购物车id',
        references: {
            model: 'shoppingcarts',
            key: 'id'
          }
      },
      pcount: {
        type: DataTypes.INTEGER,
        defaultValue: 1,
        comment: '商品数量'
      },
      sprice: {
        type: DataTypes.DECIMAL,
        defaultValue: 0.00,
        comment: '商品实价'
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '创建时间'
      },
      updatedAt: { 
        type: DataTypes.DATE,
        allowNull: true,
        comment: '更新时间'
      }
    }, {
      tableName: 'shoppingcarts_products'
    });
  
    Shoppingcarts_products.associate = function() {
      app.model.Shoppingcarts.belongsToMany(app.model.Products,
        {
          through: app.model.ShoppingcartsProducts,
          foreignKey: "sid",
          otherKey: "pid"
        })
      app.model.Products.belongsToMany(app.model.Shoppingcarts,
        {
          through: app.model.ShoppingcartsProducts,
          foreignKey: "pid",
          otherKey: "sid"
        })

        // app.model.Shoppingcarts.belongsToMany(app.model.ShoppingcartsProducts, {
        //   through: app.model.ShoppingcartsProducts,
        //   foreignKey: "sid",
        //   otherKey: "pcount",
        // });
  
    }
  
    return Shoppingcarts_products;
  };
  